<?php
/**
 * Usage :
 *
 * resources.doctrine.connections.1.dbtype = mysql
 * resources.doctrine.connections.1.host = localhost
 * resources.doctrine.connections.1.port = 3306
 * resources.doctrine.connections.1.username = root
 * resources.doctrine.connections.1.password = pass
 * resources.doctrine.connections.1.database = databasename
 *
 *
 * @author Loïc Frering <loic.frering@atosorigin.com>
 */
class Atos_Application_Resource_Doctrine extends Zend_Application_Resource_ResourceAbstract
{
    public function init()
    {
        $options = $this->getOptions();
        Zend_Loader_Autoloader::getInstance()
            ->registerNamespace('Doctrine')
            ->setFallbackAutoloader(true);
        set_include_path(APPLICATION_PATH . '/models' . PATH_SEPARATOR . APPLICATION_PATH . '/models/generated' . PATH_SEPARATOR . get_include_path());

        $manager = $this->_setupManager();
        $this->_setupConnections();
        if(!empty($options['manager']['currentConnection'])) {
            $manager->setCurrentConnection($options['manager']['currentConnection']);
        }
        $this->_setupDoctrineConfig();

        return $manager;
    }

    /**
     *
     * @return Doctrine_Manager
     */
    protected function _setupManager()
    {
        $options = $this->getOptions();
        $manager = Doctrine_Manager::getInstance();
        if(isset($options['manager']['attributes'])) {
            foreach($options['manager']['attributes'] as $attribute => $value) {
                $manager->setAttribute($attribute, $value);
            }
        }
        return $manager;
    }

    protected function _setupConnections()
    {
        $options = $this->getOptions();
        foreach($options['connections'] as $connectionName => $connectionOptions) {
            $connection = Doctrine_Manager::connection($this->_getConnectionString($connectionOptions), $connectionName);
            if(isset($connectionOptions['attributes'])) {
                foreach($connectionOptions['attributes'] as $attribute => $value) {
                    $connection->setAttribute($attribute, $value);
                }
            }
        }
    }

    protected function _setupDoctrineConfig()
    {
        $options = $this->getOptions();
        $doctrineConfig = array(
            'data_fixtures_path' => isset($options['data_fixtures_path']) ? $options['data_fixtures_path'] : APPLICATION_PATH . '/doctrine/data/fixtures',
            'models_path'        => isset($options['models_path']) ? $options['models_path'] : APPLICATION_PATH . '/models',
            'migrations_path'    => isset($options['migrations_path']) ? $options['migrations_path'] : APPLICATION_PATH . '/doctrine/migrations',
            'sql_path'           => isset($options['sql_path']) ? $options['sql_path'] : APPLICATION_PATH . '/doctrine/data/sql',
            'yaml_schema_path'   => isset($options['yaml_schema_path']) ? $options['yaml_schema_path'] : APPLICATION_PATH . '/doctrine/schemas'
        );
        $this->getBootstrap()->getContainer()->set('doctrine_config', $doctrineConfig);

        return $doctrineConfig;
    }

    protected function _getConnectionString($options)
    {
        if(isset($options['connectionString'])) {
            return $options['connectionString'];
        }
        else {
            return $options['dbtype']
                . '://'
                . $options['username']
                . ':'
                . $options['password']
                . '@'
                . $options['host']
                . '/'
                . $options['database'];
        }
    }
}